package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.model.Emp;
import com.yh.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * ClassName:EmpController
 * Package:com.yh.controller
 * Time: 2024/6/6
 */

@Controller("empController")
public class EmpController {

    @Autowired
    private EmpService empService;

    public void findByPage() {
        //设置分页标准
        PageHelper.startPage(1, 4);
        List<Emp> emps = empService.findByEmp(null);
        emps.forEach(System.out::println);

        //显示分页信息
        PageInfo<Emp> pageInfo = new PageInfo<>(emps);

        System.out.println("总记录数：" + pageInfo.getTotal());
        System.out.println("总页数：" + pageInfo.getPages());
        System.out.println("当前页：" + pageInfo.getPageNum());
        System.out.println("每页记录数：" + pageInfo.getPageSize());
        System.out.println("是否有前一页：" + pageInfo.isHasPreviousPage());
        System.out.println("是否有下一页：" + pageInfo.isHasNextPage());
        System.out.println("前一页：" + pageInfo.getPrePage());
        System.out.println("后一页：" + pageInfo.getNextPage());
    }

    public void addEmp(Emp emp) {

        int i = empService.addEmp(emp);
        System.out.println("成功添加了" + i + "条数据");
        System.out.println("返回新添加对象的信息:" +"\n"+ emp);

    }

    public void deleteEmp(List<Long> id) {
        int i = empService.deleteEmp(id);
        System.out.println("成功删除了" + i + "条数据");
    }

    public void updateEmp(Emp emp) {
        int i = empService.updateEmp(emp);
        System.out.println("成功更新了" + i + "条数据");
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }
}
